<template>
  <div class="teacherBaseInfo">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>基础信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>教师基本信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="20" class="mb-12">
      <el-col :span="4">
        <el-select
          class="full-line"
          v-model="teacherInfoSearchForm.majorId"
          placeholder="请选择专业"
          filterable
          remote
          clearable>
          <el-option
            v-for="item in majorChoose"
            :label="item.majorName"
            :value="item.majorID">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-input v-model="teacherInfoSearchForm.teacherName" placeholder="请输入教师姓名"></el-input>
      </el-col>
      <el-col :span="4">
        <el-select v-model="teacherInfoSearchForm.teacherSex" placeholder="请选择性别" class="full-line" clearable>
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="teacherInfoSearchForm.degree" placeholder="请选择学位" class="full-line" clearable>
          <el-option label="学士" value="学士"></el-option>
          <el-option label="硕士" value="硕士"></el-option>
          <el-option label="博士" value="博士"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="teacherInfoSearchForm.positionalTitle" placeholder="请选择职称" class="full-line" clearable>
          <el-option label="助教" value="助教"></el-option>
          <el-option label="讲师" value="讲师"></el-option>
          <el-option label="副教授" value="副教授"></el-option>
          <el-option label="教授" value="教授"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4" class="overflow-clear">
        <el-button type="primary" @click="searchInfo" class="float-right">立即搜索</el-button>
      </el-col>
    </el-row>
    <hr>
    <el-row :gutter="20">
      <el-col :span="4">
        <el-tree
          accordion
          :data="tree"
          :props="defaultProps"
          @node-click="handleNodeClick">
        </el-tree>
      </el-col>
      <el-col :span="20">
        <el-table
          :data="teacherTable"
          border
          style="width: 100%">
          <el-table-column
            prop="teacherId"
            label="教师编号"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="teacherName"
            label="姓名"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="teacherSex"
            label="性别"
            min-width="100">
          </el-table-column>
          <el-table-column
            prop="degree"
            label="学位"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="teacherPosition"
            label="职务"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="positionalTitle"
            label="职称"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="joinSchoolTime"
            label="入校时间"
            min-width="120">
          </el-table-column>
          <el-table-column
            prop="teacherTel"
            label="联系方式"
            min-width="150">
          </el-table-column>
          <el-table-column
            min-width="120"
            label="操作">
            <template scope="scope">
              <el-button type="primary" @click="toPasswordDialog">创建账户</el-button>
        	  <el-button type="primary" @click="deleteRow">删除账户</el-button>
              <el-button icon="edit" type="text" @click="updateTeacherInfo(scope.row.teacherId)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="paging">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog title="初始密码" v-model="dialogFormVisible">
      <el-form :model="passwordForm">

        <el-form-item label="初始密码" :label-width="formLabelWidth">
          <el-input v-model="passwordForm.password"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="initPassWord">确定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
</style>
<script>
  export default{
    name: 'teacherBaseInfo',
    data(){
      return {
        teacherInfoSearchForm: {
          majorId: '',
          teacherName: '',
          teacherSex: '',
          degree: '',
          positionalTitle: '',
          id: ''
        },
        passwordForm:{
          password:'',
        },
        tree: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        teacherTable: [],
        majorChoose: [],
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 30, 40],
        total: 40,
        searchStatus: 0,
        dialogFormVisible: false,
        formLabelWidth: '120px'
      }
    },
    components: {},
    mounted: function () {
      this.$nextTick(function () {
        this.init();
      })
    },
    methods: {
      init(){
        this.getTeacherInfo();
        this.getMajorInfo();
        this.getTree();
      },
      getMajorInfo: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/initData').then(response => {
          this.majorChoose = response.body.majorAndId
        }, response => {
          console.log('文件获取失败');
        });
      },
      getTree: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/getOneTree').then(response => {
          this.tree = response.body.xyList
        }, response => {
          console.log('文件获取失败');
        });
      },
      searchInfo(){
        this.teacherInfoSearchForm.id = '';
        this.teacherInfoSearchForm.upId = '';
        this.teacherInfoSearchForm.majorID = '',
          this.teacherInfoSearchForm.enrollmentTime2 = '',
          this.teacherInfoSearchForm.collegeID = '',
          this.teacherInfoSearchForm.classID = ''
        this.getTeacherInfo();
      },
      handleNodeClick(data) {
        //console.log(data);
        for (var key in this.teacherInfoSearchForm) {
          this.teacherInfoSearchForm[key] = "";
        }
        for (var value of this.tree) {
          //console.log(value)
          if (value.id == data.id) {
            this.teacherInfoSearchForm.id = data.id
            this.teacherInfoSearchForm.collegeID = data.id
            this.getTeacherInfo();
            //console.log(1)
            return
          }
          for (var item of value.children) {
            if (item.id == data.id) {
              this.teacherInfoSearchForm.upId = value.id
              this.teacherInfoSearchForm.id = data.id
              this.teacherInfoSearchForm.collegeID = value.id
              this.teacherInfoSearchForm.majorID = data.id
              this.getTeacherInfo();
              //console.log(2)
              return
            }
            if (item.children) {
              for (var msg of item.children) {
                if (msg.id == data.id) {
                  this.teacherInfoSearchForm.upId = item.id
                  this.teacherInfoSearchForm.id = data.id
                  this.teacherInfoSearchForm.majorID = item.id
                  this.teacherInfoSearchForm.enrollmentTime2 = data.id
                  this.getTeacherInfo();
                  //console.log(3)
                  return
                }
                if (msg.children) {
                  for (var ele of msg.children) {
                    if (ele.id == data.id) {
                      this.teacherInfoSearchForm.upId = msg.id
                      this.teacherInfoSearchForm.id = data.id
                      this.teacherInfoSearchForm.enrollmentTime2 = msg.id
                      this.teacherInfoSearchForm.classID = data.id
                      this.getTeacherInfo();
                      //console.log(4)
                      return
                    }
                  }
                }
              }
            }
          }
        }
      },
      getTeacherInfo(){
        this.$http.post(this.$until.url + '/teacherBaseInfo/getTeacher', this.teacherInfoSearchForm,
          {
            params: {
              page: this.currentPage,
              pageSize: this.pageSize
            },
            emulateJSON: true
          }).then(response => {
          this.total = response.body.countNum;
          this.teacherTable = response.body.teacherList
        }, response => {
          console.log('文件获取失败');
        });
      },
      updateTeacherInfo(teacherId){
        this.$router.push({name: 'TeacherPersonInfoFix', params: {teacherId: teacherId}})
      },
      toPasswordDialog(){
        this.dialogFormVisible = true
      },
      initPassWord(){
      	  this.studentInfoSearchForm.password = this.passwordForm.password;
      	  alert(this.passwordForm.password);
      	  console.log(this.studentInfoSearchForm);
	      this.$http.post(this.$until.url + '/teacherManage/createTUser', this.studentInfoSearchForm
	          ).then(response => {
	          alert("创建账户成功!");
	          this.dialogFormVisible = false;
	        }, response => {
	          console.log('文件获取失败');
	        });
      },
      deletePassWord(){
	      this.$http.post(this.$until.url + '/teacherManage/deleteTUser', this.studentInfoSearchForm
	          ).then(response => {
	          alert("删除账户成功!");
	        }, response => {
	          console.log('文件获取失败');
	        });
      },
      deleteRow: function (index, rows, teacherId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRes(teacherId);
          rows.splice(index, 1);
          this.total = this.total - 1;
          //
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      deleteRes: function (teacherId) {
        this.$http.get(this.$until.url + '/teacherBaseInfo/deleteTeacher',
          {
            params: {
              teacherId: teacherId
            }
          }
        ).then(response => {
          this.getTeacherInfo()
          this.$message({
            type: response.body.message,
            message: '删除成功!'
          });
        }, response => {
          alert("找不到delete文件");
        });
      },
      toTeacherPersonInfo(){
        this.$router.push({path: 'TeacherPersonInfo'})
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.getTeacherInfo()

      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getTeacherInfo()
      },
      upload(){
        var fd = new FormData(), input;
        input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.setAttribute('name', 'file');
        document.body.appendChild(input);
        input.style.display = 'none';
        input.click();
        input.onchange = () => {
          fd.append('file', input.files[0]);
          this.uploadFile(fd)
        }
      },
      uploadFile(fd){
        this.$http.post(this.$until.url + '/teacherBaseInfo/importTeacher', fd).then((res) => {
          this.getTeacherInfo()
          this.$message({
            message: '文件上传成功',
            type: 'success'
          });
        }, (err) => {
          this.$message.error('文件上传失败');
        });
      },
      downloadFile(){
        this.$http.get(`${this.$until.url}/teacherBaseInfo/exportTeacher`, {
          params: this.teacherInfoSearchForm
        }).then((res) => {
          var url = res.body.message
          try {
            var elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
          } catch (e) {
          }
        }, (err) => {

        })
      },
      printInfo(){
        var msg = '';
        for (var key in this.teacherInfoSearchForm) {
          if (this.teacherInfoSearchForm[key] == '') {
            continue;
          }
          msg += `&${key}=${this.teacherInfoSearchForm[key]}`
        }
        //console.log(msg)
        window.open(`${this.$until.url}/ReportServer?reportlet=printTeacher.cpt${msg}`)
      }
    }
  }
</script>
